﻿using System;
using System.Collections.Generic;
using System.Linq;
namespace WCF.Common.DAL
{
    public class RoleObjectDAL : BaseDAL
    {
        /// <summary>
        /// 获取表数据
        /// </summary>
        /// <returns>List</returns>
        public List<RoleObject> Gets()
        {
            return GetDataContext()
                .RoleObject
                .ToList();
        }

        /// <summary>
        /// 增加数据
        /// </summary>
        /// <param name="role"></param>
        /// <returns>OperateResult</returns>
        public OperateResult Insert(RoleObject role)
        {
            try
            {
                var context = GetDataContext();
                context.RoleObject.InsertOnSubmit(role);
                context.SubmitChanges();
                return new OperateResult() { IsSuccess = true };
            }
            catch (Exception ex)
            {
                return new OperateResult() { IsSuccess = false, Message = ex.Message };
            }
        }

        /// <summary>删除数据</summary>
        /// <param name="role"></param>
        /// <returns>OperateResult</returns>
        public OperateResult Delete(string id)
        {
            try
            {
                var context = GetDataContext();

                var obj = this.Gets().Single(p => p.ID == id);
                context.RoleObject.DeleteOnSubmit(obj);

                context.SubmitChanges();
                return new OperateResult() { IsSuccess = true };
            }
            catch (Exception ex)
            {
                return new OperateResult() { IsSuccess = false, Message = ex.Message };
            }
        }

        /// <summary>
        /// 修改数据
        /// </summary>
        /// <param name="role"></param>
        /// <returns>OperateResult</returns>
        public OperateResult Update(RoleObject role)
        {
            try
            {
                var context = GetDataContext();

                var obj = this.Gets().Single(p => p.ID == role.ID);
                obj.Caption = role.Caption;
                obj.Code = role.Code;
                obj.Describe = role.Describe;
                context.SubmitChanges();
                return new OperateResult() { IsSuccess = true };
            }
            catch (Exception ex)
            {
                return new OperateResult() { IsSuccess = false, Message = ex.Message };
            }
        }
    }
}